using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;
using System.Configuration;


namespace DAL
{
    public class tbTraLoiDAL
    {
          private SqlConnection cn = null;
        public tbTraLoiDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbTraLoiDAL(tbTraLoiDAL tan)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
       
        public void Insert(tbTraLoi th)
        {
            SqlCommand cm = new SqlCommand("tbTraLoi_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@binhluan", th.binhluan);
            cm.Parameters.AddWithValue("@khachhang", th.khachhang);
            cm.Parameters.AddWithValue("@traloi", th.traloi); 
            cm.ExecuteNonQuery();
        }
        public void Update(tbTraLoi th)
        {
            SqlCommand cm = new SqlCommand("tbTraLoi_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@binhluan", th.binhluan);
            cm.Parameters.AddWithValue("@khachhang", th.khachhang);
            cm.Parameters.AddWithValue("@traloi", th.traloi);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("tbTraLoi_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@matrl", iD);
            cm.ExecuteNonQuery();
        }
        public tbTraLoiCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbTraLoi_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTraLoiCollection traloiCollection = new tbTraLoiCollection();

            while (dr.Read())
            {
               tbTraLoi th = new tbTraLoi();
                th.matrl = int.Parse(dr.GetValue(0).ToString());
                th.binhluan = int.Parse(dr.GetValue(1).ToString());
                th.khachhang = int.Parse( dr.GetValue(2).ToString());
                traloiCollection.Add(th);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return traloiCollection;
        }
    }
}
